﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Pottery.Frontend.Models;
using Softserve.Pottery.Infrastructure;
using Softserve.Pottery.Store.Domains;

namespace Pottery.Frontend.ViewModels
{
    public class CartViewModel : IAssignable
    {
        #region IAssignable implemintation
        
        public void AssignTo(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();

            if (obj is CartViewModel)
            {
                CartViewModel model = obj as CartViewModel;
                model.Cart = Cart;
            }

            throw new NotSupportedException();
        }

        #endregion

        #region Public logic

        public static CartViewModel FromDomainModel(Cart cart)
        {
            if (cart == null)
                return null;

            return new CartViewModel
            {
                Cart = cart
            };
        }

        #endregion
        
        #region Properties

        public Cart Cart { get; set; }

        public decimal Total 
        {
            get { return Cart.Lines.Sum(l => l.Product.Price * l.Quantity); } 
        }
        #endregion 
    }
}